//
//  Utilities.m
//  This file contains classes that are used can
//      be used for solving gas related problems
//
//  The following are the class names:
//      DiveCalcs -- Added 21/04/11
//      GasMix -- Added 21/04/11
//
//
//  Created by Donald MacKay on 11-04-21.
//  Copyright 2011 H2OWorx.com. All rights reserved.
//

#import "Utilities.h"
/**
 *  Class DiveCalcs - Includes a collection of class methods
 *      that can be called for general purpose problems.
 *
 *  Includes: ENDOfGas - Calculates the Equivalent Narcotic Depth
 *            EADOfGas - Calculates the Equivalent Air Depth
 **/

@implementation DiveCalcs

- (id)init
{
    self = [super init];
    if (self) {
        // Initialization code here.
    }
    
    return self;
}
/***
 * -(long) ENDofGas: (double) fractionOfHelium atDepthOf: (double) depth
 *      This method simply calculates the equivalent narcotic
 *      depth based on the provided input
 *
 *  Input: (double) fractionOfHelium - the fHe of the gas to be used 
 *         (double) atDepth - the depth of the intended gas use
 *         (BOOL) isMetric - TRUE if metric
 *
 *  Returns: A double value representing the END
 */
+(long) ENDofGas: (double) fractionOfHelium atDepthOf: (double) depth isMetric: (BOOL) metric {

    double ata = (depth / (metric ? 10 : 33)) + 1;
    double fractionOfNarcoticGases = 1 - fractionOfHelium;
    double ppOfNarcoticGases = ata * fractionOfNarcoticGases;
    
    return (long)((ppOfNarcoticGases - 1) * (metric ? 10 : 33));
}

/***
 * -(double) EADofGas: (double) fractionOfNitrogen atDepthOf: (double) depth
 *      This method simply calculates the equivalent air
 *      depth based on the provided input
 *
 *  Input: (double) fractionOfNitrogen - the fN2 of the gas to be used 
 *         (double) atDepth - the depth of the intended gas use 
 *
 *  Returns: A double value representing the EAD
 */
+(long) EADofGas: (double) fractionOfNitrogen atDepthOf: (double) depth isMetric: (BOOL) metric {    

    return (long)(((fractionOfNitrogen * (depth + (metric ? 10 : 33))) / 0.79) - (metric ? 10 : 33));
}

- (void)dealloc
{
    [super dealloc];
}

@end



/***
 * Class GasMix
 *      This class models a gas blend by tracking the O2 and Helium
 *      content of the mix
 *
 *  Created: 21/04/11 DBM
 *
 *  Methods: -(double) MOD
 *           -(double) ppO2
 *           -(double) fO2
 *
 *  Revision History
 */
@implementation GasMix

@synthesize oxygenContent, heliumContent, nitrogenContent;

- (id)initAsNitrox: (double) withFractionOfO2  {
    self = [super init];
    if (self) {
        oxygenContent = withFractionOfO2;
    }
    return self;
}

- (id)initAsTriMixWithFO2Of: (double) fO2 andFractionOfHelium:(double) fHe {
    self = [super init];
    if (self) {
        oxygenContent = fO2;
        heliumContent = fHe;
        nitrogenContent = 1 - (oxygenContent + heliumContent);
    }
    return self;
}

-(double) MOD: (double) atPPO2 isMetric: (BOOL) metric {    
    return ((atPPO2 / self.oxygenContent) - 1) * (metric ? 10 : 33);
}

-(double) ppO2: (long) atDepth isMetric: (BOOL) metric {
    double ata = (atDepth / (metric ? 10 : 33)) + 1;
    return ((oxygenContent * ata) - 1)/(metric ? 10 : 33);
}

-(double) fO2{
    return 0.0;
    
}
@end 